package app.Disthanitian.thaistyle.EmerMez;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MyAccount extends Activity {

	public static final String url = "http://192.168.2.43/emermez/myaccount.php";

	public static String result = "";
	InputStream is = null;
	String js_result = "";
	HttpPost httppost = null;

	TextView email;
	TextView password;
	TextView firstname;
	TextView lastname;
	TextView phone;
	TextView pincode;

	protected void onCreate(Bundle saveInstanceState) {
		super.onCreate(saveInstanceState);
		setContentView(R.layout.myaccount);

		// Button bt_show = (Button) findViewById(R.id.show);
		ShowMyAccount();

		/*
		 * bt_show.setOnClickListener(new OnClickListener() {
		 * 
		 * public void onClick(View arg0) { ShowMyAccount(); } });
		 */

	}

	public String ShowMyAccount() {

		email = (TextView) findViewById(R.id.viewEmail);
		password = (TextView) findViewById(R.id.viewPassword);
		firstname = (TextView) findViewById(R.id.viewFirstname);
		lastname = (TextView) findViewById(R.id.viewLastname);
		phone = (TextView) findViewById(R.id.viewPhone);
		pincode = (TextView) findViewById(R.id.viewPincode);

		try {
			HttpClient httpclient = new DefaultHttpClient();
			httppost = new HttpPost(url);
			HttpResponse response = httpclient.execute(httppost);
			HttpEntity entity = response.getEntity();

			is = entity.getContent();

		} catch (Exception e) {
			Log.e("log_err", "Error in http connection !!! " + e.toString());
		}

		try {
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					is, "UTF-8"));
			// new InputStreamReader(is,"iso-8859-1"),8
			// new InputStreamReader(is, "UTF-8")

			StringBuilder sb = new StringBuilder();
			String line;
			while ((line = reader.readLine()) != null) {
				sb.append(line);
			}
			is.close();
			js_result = sb.toString();
			return js_result;

			// System.out.println("get data show  >>>>> "+sb.toString());
		} catch (Exception e) {
			Log.e("log_tag", "Error converting result " + e.toString());
		}
		try {

			String json = "[{'id':'3','email':'mail','password':'1234','firstname':'r','lastname':'j','phonenum':'0818614475'}]";

			// Toast.makeText(MyAccount.this,js_result
			// ,Toast.LENGTH_LONG).show();

			JSONArray jArray = new JSONArray(json);

			for (int i = 0; i < jArray.length(); i++) {
				JSONObject jo = jArray.getJSONObject(i);

				String email1 = jo.getString("email");
				String password1 = jo.getString("password");
				String firstname1 = jo.getString("firstname");
				String lastname1 = jo.getString("lastname");
				String phone1 = jo.getString("phonenum");
				// String pincode1 = jo.getString("pincode");
				// Log.d("log", hn + "," + name + "," + age + "," + date_serv);

				email.append(": " + email1);
				password.append(": " + password1);
				firstname.append(": " + firstname1);
				lastname.append(": " + lastname1);
				phone.append(": " + phone1);
				// pincode.append(" "+pincode1);
			}
		} catch (JSONException e) {
			Log.e("log_tag", "Error parsing data " + e.toString());
		}
		return null;

	}

}
